package com.dycong.common.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 作用描述:
 * 给定一个三角形，找出自顶向下的最小路径和。每一步只能移动到下一行中相邻的结点上。
 * <p>
 * 例如，给定三角形：
 * <p>
 * [
 * [2],
 * [3,4],
 * [6,5,7],
 * [4,1,8,3]
 * ]
 * 自顶向下的最小路径和为 11（即，2 + 3 + 5 + 1 = 11）。
 * <p>
 * 说明：
 * <p>
 * 如果你可以只使用 O(n) 的额外空间（n 为三角形的总行数）来解决这个问题，那么你的算法会很加分。
 *
 * @author dycong
 * @date 2019/12/17 9:11
 */
public class MinimumTotal_120 {

    public static void main(String[] args) {
        MinimumTotal_120 minimumTotal_120 = new MinimumTotal_120();
        List<Integer> list_1 = new ArrayList<>();
        list_1.add(2);
        List<Integer> list_2 = new ArrayList<>();
        list_2.add(3);
        list_2.add(4);
        List<Integer> list_3 = new ArrayList<>();
        list_3.add(6);
        list_3.add(5);
        list_3.add(7);
        List<Integer> list_4 = new ArrayList<>();
        list_4.addAll(Arrays.asList(4, 1, 8, 3));
        List<List<Integer>> triangle = new ArrayList<>();
        triangle.add(list_1);
        triangle.add(list_2);
        triangle.add(list_3);
        triangle.add(list_4);
        System.out.println(minimumTotal_120.minimumTotal3(triangle));
    }

    /**
     * 动态规划-自底向上
     *
     * @param triangle
     * @return
     */
    public int minimumTotal(List<List<Integer>> triangle) {
        if (triangle.isEmpty()) {
            return 0;
        }
        if (triangle.size() == 1) {
            return triangle.get(0).get(0);
        }
        return minimumTotal2(triangle);
    }

    /**
     * 动态规划-自底向上
     *
     * @param triangle
     * @return
     */
    public int minimumTotal2(List<List<Integer>> triangle) {
        int lastLevelSize = triangle.get(triangle.size() - 1).size();
        int[] ints = new int[lastLevelSize];
        List<Integer> lastLevel = triangle.get(triangle.size() - 1);
        for (int i = 0; i < ints.length; i++) {
            ints[i] = lastLevel.get(i);
        }

        for (int i = triangle.size() - 2; i >= 0; i--) {
            List<Integer> level = triangle.get(i);

            for (int j = 0; j < level.size(); j++) {
                int l_j = level.get(j) + Math.min(ints[j], ints[j + 1]);
                ints[j] = l_j;
            }
        }
        return ints[0];
    }

    /**
     * 递归，自顶向下
     *
     * @param triangle
     * @return
     */
    public int minimumTotal3(List<List<Integer>> triangle) {
        int len = triangle.size();
        int row = 0;
        int col = 0;
        return helper(len, row, col, triangle);
    }

    public int helper(int len, int row, int col, List<List<Integer>> triangle) {
        //截至条件：最后一行时截止
        if (row == len - 1) {
            return triangle.get(row).get(col);
        }
        int left = helper(len, row + 1, col, triangle);
        int right = helper(len, row + 1, col + 1, triangle);
        return Math.min(left, right) + triangle.get(row).get(col);
    }

    public int minimumTotal4(List<List<Integer>> triangle) {
        int len = triangle.size();
        int row = 0;
        int col = 0;
        return helper2(len, row, col, triangle);
    }

    public int helper2(int len, int row, int col, List<List<Integer>> triangle) {
        if (row == len - 1) {
            return triangle.get(row).get(col);
        }
        int left = helper2(len, row + 1, col, triangle);
        int right = helper2(len, row + 1, col + 1, triangle);
        return Math.min(left, right) + triangle.get(row).get(col);
    }

}
